﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Skewworks.Win.Motif.Glide
{
    public partial class GlideButton : UserControl, IEmbeddedControl
    {

        private string _text;
        private GlideFonts _font;
        private StringAlignment _ha;
        private Color _tint;
        private byte _tintAmount;
        private Color _foreColor;
        private Color _disabled;

        private ISettings _settings;

        public GlideButton()
        {
            InitializeComponent();
            _font = GlideFonts.Droid_12;
            this.Font = GlideFont.DroidSans(12);
            _ha = StringAlignment.Center;
            _foreColor = Color.Black;
            _tint = Color.Black;
            _tintAmount = 0;
            _settings = new GlideButtonSettings(this);
            _disabled = Color.DarkGray;
            this.Width = 80;
            this.Height = 32;
        }

        public override string Text
        {
            get { return _text; }
            set { _text = value; Invalidate(); }
        }

        public GlideFonts GFont
        {
            get { return _font; }
            set
            {
                _font = value;
                this.Font = GlideFont.DroidSans((int)value);
                Invalidate();
            }
        }

        public Color FontColor
        {
            get { return _foreColor; }
            set { _foreColor = value; Invalidate(); }
        }

        public Color DisabledFontColor
        {
            get { return _disabled; }
            set { _disabled = value; Invalidate(); }
        }

        public Color TintColor
        {
            get { return _tint; }
            set { _tint = value; Invalidate(); }
        }

        public byte TintAmount
        {
            get { return _tintAmount; }
            set { _tintAmount = value; Invalidate(); }
        }

        public ISettings Settings
        {
            get { return _settings; }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            lock (this)
            {
                Color c1 = Color.FromArgb(212, 212, 212);
                Brush bsh1 = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height - 1), c1, Color.FromArgb(102, 102, 102), 90F);
                Brush bsh2 = new LinearGradientBrush(new Rectangle(1, 1, this.Width - 1, this.Height - 2), Color.White, c1, 90F);
                Pen pn = new Pen(bsh1);
                Pen pnBtm = new Pen(c1);

                e.Graphics.FillRectangle(bsh2, new Rectangle(0, 0, this.Width - 1, this.Height - 2));
                e.Graphics.DrawRectangle(pn, new Rectangle(0, 0, this.Width - 1, this.Height - 2));
                e.Graphics.DrawLine(pnBtm, 0, this.Height - 1, this.Width, this.Height - 1);

                if (_tintAmount != 0)
                    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(_tintAmount, _tint)), new Rectangle(0, 0, this.Width - 1, this.Height - 2));

                StringFormat SF = new StringFormat();
                SF.Alignment = _ha;
                e.Graphics.DrawString(_text, this.Font, new SolidBrush(_foreColor), new Rectangle(0, this.Height / 2 - this.Font.Height / 2, this.Width, this.Font.Height), SF);

                if (Focused)
                {
                    e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(0, 0, Width - 1, Height - 1));
                    e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(1, 1, Width - 3, Height - 3));
                }
            }
        }

        #region Moving/Sizing

        private bool _sizeV, _sizeH, _move;
        private Point _sizeRef;

        private void GlideButton_MouseMove(object sender, MouseEventArgs e)
        {
            if (_sizeH || _sizeV)
            {
                if (_sizeH)
                    this.Width += e.X - _sizeRef.X;
                if (_sizeV)
                    this.Height += e.Y - _sizeRef.Y;
                Invalidate();
            }
            else if (_move)
            {
                this.Left += e.X - _sizeRef.X;
                this.Top += e.Y - _sizeRef.Y;
                return;
            }
            else
            {
                if (e.X >= this.Width - 8)
                {
                    if (e.Y >= this.Height - 8)
                        this.Cursor = Cursors.SizeNWSE;
                    else
                        this.Cursor = Cursors.SizeWE;
                }
                else if (e.Y >= this.Height - 8)
                    this.Cursor = Cursors.SizeNS;
                else
                    this.Cursor = Cursors.Default;
            }
            _sizeRef = new Point(e.X, e.Y);
        }

        private void GlideButton_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.X >= this.Width - 8)
            {
                if (e.Y >= this.Height - 8)
                {
                    _sizeH = true;
                    _sizeV = true;
                }
                else
                    _sizeH = true;
            }
            else if (e.Y >= this.Height - 8)
                _sizeV = true;
            else
            {
                _move = true;
                this.Cursor = Cursors.SizeAll;
            }
        }

        private void GlideButton_MouseUp(object sender, MouseEventArgs e)
        {
            _sizeH = false;
            _sizeV = false;
            _move = false;
            this.Cursor = Cursors.Default;
        }

        #endregion

    }
}
